package com.dimples.dd.web.framework.web.config;


import com.dimples.dd.common.constant.DDConstant;
import com.dimples.dd.common.enums.WebFilterOrderEnum;
import com.dimples.dd.web.framework.web.core.filter.RequestBodyCacheFilter;
import jakarta.servlet.Filter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@AutoConfiguration
public class WebAutoConfiguration implements WebMvcConfigurer {

    // ========== Filter 相关 ==========

    /**
     * 创建 RequestBodyCacheFilter Bean，可重复读取请求内容
     */
    @Bean
    public FilterRegistrationBean<RequestBodyCacheFilter> requestBodyCacheFilter() {
        log.info(DDConstant.CONFIG_LOG, "Add Filter RequestBodyFilter");
        return createFilterBean(new RequestBodyCacheFilter(), WebFilterOrderEnum.REQUEST_BODY_CACHE_FILTER);
    }


    public static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
        FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
        bean.setOrder(order);
        return bean;
    }

}
